package com.wf.smartcomm.server.serviceImpl;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Shop;
import com.wf.smartcomm.server.bean.Village;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.ShopService;
import com.wf.smartcomm.server.util.IdUtils;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-20 下午3:55:36 
 * 
 */
@Service
@Transactional
public class ShopServiceImpl implements ShopService{

	@Resource
	private SessionFactory factory;
	
	@Override
	public void save(Shop shop) {
		shop.setObjectId(IdUtils.uuid());
		shop.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		shop.setUpdateAt(shop.getCreatedAt());
		factory.getCurrentSession().persist(shop);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void listByVillage(Village village, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Shop where village = :village");
		query.setParameter("village", village);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Shop shop where shop.village = :village order by updateAt desc");
		query.setParameter("village", village);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	public void update(Shop shop) {
		shop.setUpdateAt(String.valueOf(System.currentTimeMillis()));
		factory.getCurrentSession().update(shop);
	}

	@Override
	public void delete(Shop shop) {
		factory.getCurrentSession().delete(shop);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void query(Village loginVillage, String sSearch, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Shop where village =:village and (subject like :key or summary like :key)");
		query.setParameter("village", loginVillage);
		query.setParameter("key", "%"+sSearch+"%");
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Shop where village =:village and (subject like :key or summary like :key) order by updateAt desc");
		query.setParameter("village", loginVillage);
		query.setParameter("key", "%"+sSearch+"%");
		query.setFirstResult(page.getBeginIndex());
		query.setMaxResults(page.getPageSize());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void listByType(Village loginVillage, String type, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Shop where village =:village and type =:type");
		query.setParameter("village", loginVillage);
		query.setParameter("type", type);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Shop where village =:village and type =:type order by updateAt desc");
		query.setParameter("village", loginVillage);
		query.setParameter("type", type);
		query.setFirstResult(page.getBeginIndex());
		query.setMaxResults(page.getPageSize());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Shop get(String objectId) {
		Query query = factory.getCurrentSession().createQuery("from Shop where objectId =:id");
		query.setParameter("id", objectId);
		return (Shop) query.uniqueResult();
	}
}
